package cn.kevinking.ai.mcp.provider.heartbeat;

import cn.kevinking.ai.common.model.ServerInfo;
import cn.kevinking.ai.mcp.provider.McpProvider;
import cn.kevinking.ai.mcp.provider.heartbeat.req.BeatReq;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * tell mcp server which provider send the beat
 *
 * @author kevinking
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class BeatInfo extends BeatReq {
    private static final long DEFAULT_BEAT_PERIOD_MILLS = 5000L;
    private String serverName;
    private String serverUrl;
    private BeatStatus status;

    public boolean isReady() {
        return BeatStatus.READY.equals(this.status);
    }

    public boolean isStopped() {
        return BeatStatus.STOPPED.equals(this.status);
    }

    public static BeatInfo create(ServerInfo serverInfo, McpProvider instance) {
        BeatInfo newBeat = new BeatInfo();
        newBeat.setProviderName(instance.getName());
        newBeat.setIp(instance.getServlet().getIp());
        newBeat.setPort(instance.getServlet().getPort());
        newBeat.setPeriod(DEFAULT_BEAT_PERIOD_MILLS);
        newBeat.setStatus(BeatStatus.READY);
        newBeat.setServerUrl(serverInfo.getUrl());
        newBeat.setServerName(serverInfo.getName());
        return newBeat;
    }
}
